<template>
  <div class="mt15 relative">
    <div class="until_title" style="transform: translate(-20px, 0)">项目进度</div>
    <div class="right info-progress-time" >
      <p>
        <span class="info-color">开售:</span>
        <span class="info-time">{{saleTime}}</span>
      </p>
      <p class="mt5">
        <span class="info-color">交楼:</span>
        <span  class="info-time">{{deliveryTime}}</span>
      </p>
    </div>
    <div class="info-progress-day">
      <div class="left info-safe-com">
        <p class="info-progress-safe">{{nodeDay.safetyDays}}</p>
        <p>连续安全生产<span style="margin-left: 10px">(天)</span></p>
      </div>
      <div class="left info-progress-line"></div>
      <div class="right info-last-com">
        <p class="info-progress-last">{{nodeDay.spaceRemainDays}}</p>
        <p>距交楼剩余<span style="margin-left: 10px">(天)</span></p>
      </div>
    </div>
    <div class="info-progress-bar">
      <div class="progress-bar-middle relative">
        <div class="progress-bar-content" v-for="(item, index) in stageNodePlans" :style="{left: `${index / 7 * 100}%`, top: index % 2 !== 0 ? '35px' : '-50px'}">
          <div v-if="index % 2 !== 0">
            <div v-if="item.point" class="progress-bar-circle" style="top: -35px; left: 28px;"></div>
            <div v-if="item.exceed && item.point" class="progress-circle" style="top: -35px; left: 28px;"></div>
            <div v-if="item.exceed && item.point" class="progress-circle1" ref="progressCircle1" style="top: -35px; left: 28px;"></div>
            <div v-if="item.exceed && item.point" class="progress-circle1" ref="progressCircle2" style="top: -35px; left: 28px;"></div>
            <p class="progress-text-down">{{item.nodeName}}</p>
            <p>{{item.startTime}}</p>
          </div>
          <div v-else>
            <div v-if="item.point" class="progress-bar-circle" style="top: 50px; left: 28px;"></div>
            <div v-if="item.exceed && item.point" class="progress-circle" style="top: 50px; left: 28px;"></div>
            <div v-if="item.exceed && item.point" class="progress-circle1" ref="progressCircle1" style="top: 50px; left: 28px;"></div>
            <div v-if="item.exceed && item.point" class="progress-circle1" ref="progressCircle2" style="top: 50px; left: 28px;"></div>
            <p>{{item.startTime}}</p>
            <p class="progress-text">{{item.nodeName}}</p>
          </div>
        </div>
        <div class="progress-bar-circle circle6" :style="{right: 0}"></div>
        <div class="progress-bar-blur" :style="{width: `${nodeDay.rate * 100}%`}"></div>
        <!--<div class="progress-circle1" ref="progressCircle2"></div>-->
      </div>
    </div>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  export default {
    name: 'project-progress',
    data () {
      return {
        saleTime: '1966-01-01',
        deliveryTime: '2099-01-01',
        nodeDay: {
          rate: 0,
          safetyDays: 0,
          spaceRemainDays: 0
        },
        stageNodePlans: []
      }
    },
    computed: {
      ...mapGetters({
        currentUser: 'currentUser'
      })
    },
    mounted () {
      this.init()
    },
    activated () {
      this.init()
    },
    watch: {
      currentUser (oldItem, newItem) {
        if (this.currentUser.mid) {
          this.init()
        }
      }
    },
    methods: {
      init () {
        this.getStageNodeInfo().then(() => {
          this.circleVelocity()
          this.compareRate()
        })
      },
      circleVelocity () {
        if (this.$refs.progressCircle1) {
          this.$refs.progressCircle1.forEach(el => {
            this.$velocity(el, {
              scale: 1.75
            }, {
              loop: true,
              duration: 1000
            })
          })
        }

        if (this.$refs.progressCircle2) {
          this.$refs.progressCircle2.forEach(el => {
            this.$velocity(el, {
              scale: 2.2
            }, {
              loop: true,
              delay: 1000,
              duration: 1000
            })
          })
        }
      },
      compare (property) {
        return function (a, b) {
          return Number.parseInt(a[property].replace(/-/g, '')) - Number.parseInt(b[property].replace(/-/g, ''))
        }
      },
      compareRate () {
        let exceedLength = this.stageNodePlans.filter(item => item.exceed).length
        this.nodeDay.rate = exceedLength / 7
      },
      getStageNodeInfo () {
        return new Promise(resolve => {
          const data = {
            'stageId': this.currentUser.stageId
          }
          this.$axios({
            method: 'post',
            url: `${this.$site.videoServer}/api/screen/getStageNodeInfo`,
            data: this.$qs.stringify(data, {indices: false})
          }).then(res => {
            if (res.data.data.nodeDay) {
              this.nodeDay = res.data.data.nodeDay
            }
            let length = res.data.data.stageNodePlans.length
            if (length > 0) {
              let stageNodePlans = res.data.data.stageNodePlans.sort(this.compare('startTime'))
              let today = Number.parseInt(this.$moment().format('YYYY-MM-DD').replace(/-/g, ''))
              this.stageNodePlans = stageNodePlans.map((item, index) => {
                let startTime = Number.parseInt(item.startTime.replace(/-/g, ''))
                Object.assign(item, {exceed: today > startTime})
                if (index === 0 || index === length - 1) {
                  Object.assign(item, {point: false})
                } else {
                  Object.assign(item, {point: true})
                }
                if (Number.parseInt(item.nodeNum) === 4) {
                  this.saleTime = item.startTime
                }
                if (Number.parseInt(item.nodeNum) === 7) {
                  this.deliveryTime = item.startTime
                }
                return item
              })
              this.$nextTick(() => {
                resolve()
              })
            } else {
              this.saleTime = ''
              this.deliveryTime = ''
              this.stageNodePlans = []
              this.nodeDay = {
                rate: 0,
                safetyDays: 0,
                spaceRemainDays: 0
              }
            }
          })
        })
      }
    }
  }
</script>

<style scoped lang='scss'>
  .info-color{
    color: #34cddd;
  }
  .info-progress-time{
    position: absolute;
    right: 0;
    top: 2px;
    .info-time{
      padding-left: 5px;
      font-weight: bold;
    }
  }
  .info-progress-day{
    padding-top: 10px;
    box-sizing: border-box;
    height: 100px;
    >div{
      text-align: center;
      .info-progress-safe{
        color: #55eb92;
        font-size: 44px;
        font-weight: bolder;
        text-shadow: 0 3px 0 #2a7a5d;
        margin-bottom: 15px;
      }
      .info-progress-last{
        color: #00e8ff;
        font-size: 44px;
        font-weight: bolder;
        text-shadow: 0 3px 0 #048fa3;
        margin-bottom: 15px;
      }
    }
    .info-safe-com{
      width: 47%;
    }
    .info-last-com{
      width: 47%;
    }
    .info-progress-line{
      width: 6%;
      height: 67px;
      background-image: url("/static/images/line04.png");
      background-repeat: no-repeat;
      background-position: center;
      background-size: 1px;
    }
  }
  .info-progress-bar{
    font-size: 12px;
    text-align: center;
    margin-top: 60px;
    .progress-text{
      color: #fff;
      margin-top: 5px;
    }
    .progress-bar-middle{
      margin-top: 10px;
      width: 100%;
      height: 16px;
      background: rgba(1, 84, 92, 0.8);
      border-radius: 20px;
      .progress-bar-content{
        position: absolute;
        width: 70px;
        z-index: 2;
      }
      .progress-text-down{
        color: #fff;
        margin-bottom: 5px;
      }
      .progress-bar-blur{
        position: absolute;
        top: 0;
        left: 0;
        border-radius: 20px;
        background: #55eb92;
        height: 100%;
        z-index: 1;
      }
      .progress-circle{
        position: absolute;
        width: 16px;
        height: 16px;
        border-radius: 50%;
        background: #55eb92;
        box-shadow: 0 0 7px 2px rgba(0, 0, 0, 0.2);
      }
      .progress-circle1{
        position: absolute;
        width: 16px;
        height: 16px;
        border-radius: 50%;
        box-shadow: 0 0 5px 2px rgba(85, 235, 146, 0.3) inset;
      }
    }
    .progress-bar-circle{
      position: absolute;
      width: 16px;
      height: 16px;
      border-radius: 100%;
      background: #00e8ff;
    }
  }
</style>
